layui.config({
  base: "js/"
}).use(['form', 'layer', 'jquery', 'layedit', 'laydate'], function () {
  var form = layui.form(),
    layer = parent.layer === undefined ? layui.layer : parent.layer,
    laypage = layui.laypage,
    layedit = layui.layedit,
    laydate = layui.laydate,
    $ = layui.jquery;

  // 渲染教师列表
  $.ajax({
    url: "/api/teacher/all",
    type: "GET",
    dataType: "json",
    success: function (data) {
      var teachers = data;
      console.log(teachers[0]);
      for (var i = 0; i < teachers.length; i++)
        $('#Cos_teacher').append('<option value="' + teachers[i].Tea_id + '">'
          + teachers[i].Tea_name
          + '(' + teachers[i].Tea_id + ')' + '</option>');
      form.render();
    },
    error: function (data) {
      layer.warn("获取教师列表失败!");
    }
  });

  //创建一个编辑器
  var editIndex = layedit.build('Cos_intro');

  form.on("submit(addCourse)", function (data) {
    data.field.Cos_intro = layedit.getContent(editIndex);
    //弹出loading
    var index = layer.msg('数据提交中，请稍候', {icon: 16, time: 3000, shade: 0.8});
    $.ajax({
      url: "/api/course",
      type: "POST",
      data: data.field,
      dataType: "json",
      success: function (data) {
        console.log(data);
        layer.close(index);
        top.layer.msg("课程添加成功！");
        layer.closeAll("iframe");
        parent.location.reload();
      },
      error: function (data) {
        layer.close(index);
        top.layer.msg("课程添加失败！");
      }
    });
    return false;
  })

});
